package pers.yurwisher.rubick.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import pers.yurwisher.rubick.common.enums.tip.SystemCustomTipEnum;
import pers.yurwisher.rubick.common.utils.PudgeUtil;
import pers.yurwisher.rubick.common.wrapper.R;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yq 2021/4/19 16:56
 * @description CustomAccessDeniedHandler
 */
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler, AuthenticationEntryPoint {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        log.info("request [{}],access denied:[{}]", request.getRequestURI(), e.getMessage());
        PudgeUtil.responseJSON(response, R.fail(SystemCustomTipEnum.PERMISSION_DENIED));
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.info("request [{}],error:[{}]", request.getRequestURI(), e.getMessage());
        PudgeUtil.responseJSON(response, R.fail(SystemCustomTipEnum.CREDENTIALS_INVALID));
    }
}
